package com.tianchi.lexiang.util;

import java.lang.Thread.UncaughtExceptionHandler;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.tianchi.lexiang.deep.Home;

public class UiExceptionCatcher implements  UncaughtExceptionHandler {
	private static final String TAG = "UiExceptionCatcher";
	private Context mContext;
	
	public UiExceptionCatcher(Context context) {
		this.mContext = context;
	}
	
	@Override
	public void uncaughtException(Thread arg0, Throwable arg1) {
		try{
			Log.d(TAG, "[uncaughtException]will call send log by email ");
			Intent sendLog = new Intent("android.intent.action.APP_ERROR");
			mContext.sendBroadcast(sendLog);
		} catch (Exception e) {
			Log.e(TAG, "email apk doesn't exist, we can't send email");
		}
		
		Log.d(TAG, "[uncaughtException]",arg1);
		Intent restartPackage = new Intent(mContext,Home.class);
		restartPackage.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		Log.d(TAG, "[uncaughtException]restart home");
		mContext.startActivity(restartPackage);
//		 kill App Progress  
		android.os.Process.killProcess(android.os.Process.myPid());  
	}
	
}
